package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.CourseEva;
import com.zretc.emptraining.entity.EmpScore;
import com.zretc.emptraining.service.EmpScoreService;
import com.zretc.emptraining.serviceimpl.EmpScoreServiceImpl;

/**
 * Servlet implementation class EmpScoreServlet
 */
@WebServlet("/EmpScoreServlet")
public class EmpScoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 创建对象
	EmpScoreService empScoreService = new EmpScoreServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EmpScoreServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 判断请求内容
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("".equals(op)) {
			showAll(request, response);
		} else if ("add".equals(op)) {
			addScore(request, response);
		} else if ("upd".equals(op)) {
			updScore(request, response);
		} else if ("queryByPage".equals(op)) {
			queryByPage(request, response);
		} else if ("queryByEmpNo".equals(op)) {
			queryByEmpNo(request, response);
		} else if ("queryByTeacherNo".equals(op)) {
			queryByTeacherNo(request, response);
		} else if ("queryByDept".equals(op)) {
			queryByDept(request, response);
		}
	}

	private void queryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取查询的内容
		String empNoQuery = "";
		if (request.getParameter("empNoQuery") != null) {
			empNoQuery = request.getParameter("empNoQuery");
		}
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 7;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<EmpScore> pd = empScoreService.getEmpScoreByPage(page, pageSize, empNoQuery);
		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("empNoQuery", empNoQuery);
		// 转发
		request.getRequestDispatcher("EmpScoreList.jsp").forward(request, response);
	}

	private void queryByDept(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取查询的内容
		String empNoQuery = "";
		if (request.getParameter("empNoQuery") != null) {
			empNoQuery = request.getParameter("empNoQuery");
		}
		String sltkey = "1";
		if (request.getParameter("sltkey") != null) {
			sltkey = request.getParameter("sltkey");
		}
		String pass = "";
		if (request.getParameter("pass") != null) {
			pass = request.getParameter("pass");

		}

		// 获取查询的内容

		String deptNoQuery = request.getParameter("deptNo");
		// 转型
		int deptNo = Integer.parseInt(deptNoQuery);
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		PageData<EmpScore> pd = null;
		if (empNoQuery == "") {
			// 调用查询的方法
			pd = empScoreService.getEmpScoreDeptByEmpName(page, pageSize, deptNo, empNoQuery, pass);
		} else if ("1".equals(sltkey) && empNoQuery != "") {
			// 调用查询的方法
			pd = empScoreService.getEmpScoreDeptByEmpNo(page, pageSize, deptNo, empNoQuery, pass);
		} else if ("2".equals(sltkey) && empNoQuery != "") {
			// 调用查询的方法
			pd = empScoreService.getEmpScoreDeptByEmpName(page, pageSize, deptNo, empNoQuery, pass);
		} else if ("3".equals(sltkey) && empNoQuery != "") {
			// 调用查询的方法
			pd = empScoreService.getEmpScoreDeptByCpNo(page, pageSize, deptNo, empNoQuery, pass);
		}
		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("empNoQuery", empNoQuery);
		request.setAttribute("sltkey", sltkey);
		request.setAttribute("pass", pass);
		// 转发
		request.getRequestDispatcher("DeptScore.jsp").forward(request, response);
	}

	private void queryByTeacherNo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取查询的内容
		String empNoQuery = "";
		String courseName = "";
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		if (request.getParameter("courseName") != null) {
			courseName = request.getParameter("courseName");
		}
		if (request.getParameter("empNoQuery") != null) {
			empNoQuery = request.getParameter("empNoQuery");
		}
		String sltkey = "1";
		if (request.getParameter("sltkey") != null) {
			sltkey = request.getParameter("sltkey");

		}
		String pass = "";
		if (request.getParameter("pass") != null) {
			pass = request.getParameter("pass");

		}

		// 获取查询的内容
		String teacherNoQuery = request.getParameter("teachNo");
		// 转型
		int teacherNo = Integer.parseInt(teacherNoQuery);
		System.out.println("教师号"+teacherNo);
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		PageData<EmpScore> pd = null;
		if (empNoQuery == "") {

			// 调用查询的方法
			pd = empScoreService.getEmpScoreTeaByEmpName(page, pageSize, teacherNo, empNoQuery, pass, cpNo);
		} else if ("1".equals(sltkey) && empNoQuery != "") {
			// 调用查询的方法
			pd = empScoreService.getEmpScoreTeaByEmpNo(page, pageSize, teacherNo, empNoQuery, pass, cpNo);
		} else if ("2".equals(sltkey) && empNoQuery != "") {
			// 调用查询的方法
			pd = empScoreService.getEmpScoreTeaByEmpName(page, pageSize, teacherNo, empNoQuery, pass, cpNo);
		}

		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("empNoQuery", empNoQuery);
		request.setAttribute("sltkey", sltkey);
		request.setAttribute("pass", pass);
		request.setAttribute("courseName", courseName);
		request.setAttribute("cpNo", cpNo);
		// 转发
		request.getRequestDispatcher("EmpScoreIn.jsp").forward(request, response);
	}

	private void queryByEmpNo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取查询的内容

		int empNo= Integer.parseInt(request.getParameter("empNo"));
		System.out.println(empNo);
		String pass = "";
		if (request.getParameter("pass") != null) {
			pass = request.getParameter("pass");

		}
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 5;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<EmpScore> pd = empScoreService.getEmpScoreByEmpNo(page, pageSize, empNo, pass);
		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("pass", pass);

		// 转发
		request.getRequestDispatcher("EmpScoreBefore.jsp").forward(request, response);
	}

	private void updScore(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		// 获取文本框里的值
		int scoreNo = Integer.parseInt(request.getParameter("scoreNo"));
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = 5;
		int teacherNo = 7;
		double score = Double.parseDouble(request.getParameter("score"));
		String notes = request.getParameter("notes");
		int pass = Integer.parseInt(request.getParameter("pass"));

		EmpScore empScore = new EmpScore(scoreNo, empNo, cpNo, teacherNo, score, notes, pass);
		boolean flag = empScoreService.updEmpScore(empScore);
		// 创建gson
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		PrintWriter out = response.getWriter();
		// 打印
		out.print(jsonString);
		// 关闭
		out.close();
	}

	private void addScore(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		// 调用添加课程评价的方法
		// 获取文本框里的值
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		System.out.println(cpNo);
		int teacherNo = Integer.parseInt(request.getParameter("teacherNo"));
		System.out.println(teacherNo);
		double score = Double.parseDouble(request.getParameter("score"));
		String notes = request.getParameter("notes");
		int pass = Integer.parseInt(request.getParameter("pass"));

		EmpScore empScore = new EmpScore(0, empNo, cpNo, teacherNo, score, notes, pass);
		// 添加课程
		boolean flag = empScoreService.addEmpScore(empScore);
		// 创建gson
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		PrintWriter out = response.getWriter();
		// 打印
		out.print(jsonString);
		// 关闭
		out.close();
	}

	private void showAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取查询的内容
		String empNoQuery = "";
		if (request.getParameter("empNoQuery") != null) {
			empNoQuery = request.getParameter("empNoQuery");
		}
		System.out.println("员工号" + empNoQuery);
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 7;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		PageData<EmpScore> pd = null;
		// 获取查询的对象
		String selectKey = "员工名";
		if (request.getParameter("selectKey") != null) {
			selectKey = request.getParameter("selectKey");
		}
		if ("".equals(empNoQuery)) {
			pd = empScoreService.getEmpScoreByEmpName(page, pageSize, empNoQuery);
		} else if ("员工名".equals(selectKey)) {
			pd = empScoreService.getEmpScoreByEmpName(page, pageSize, empNoQuery);
		} else if ("员工号".equals(selectKey)) {
			int empNo = Integer.parseInt(empNoQuery);
			pd = empScoreService.getEmpScoreByEmpNo(page, pageSize, empNo);
		} else if ("课程名".equals(selectKey)) {
			pd = empScoreService.getEmpScoreByCourseName(page, pageSize, empNoQuery);
		} else if ("教员名".equals(selectKey)) {
			pd = empScoreService.getEmpScoreByTeacherName(page, pageSize, empNoQuery);
		}

		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("empNoQuery", empNoQuery);
		request.setAttribute("selectKey", selectKey);
		// 转发
		request.getRequestDispatcher("EmpScoreList.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
